
<script>
function clickTimKiem()
	{
		if(document.getElementById ('formTimKiem').style.display == 'none')
		{
			document.getElementById ('formTimKiem').style.display = 'block';
		}
		else
		{
			document.getElementById ('formTimKiem').style.display = 'none';
		}
	}

</script>
<?
	function word_split($str,$words) {
	$arr = preg_split("/[\s]+/", $str,$words+1);
	$arr = array_slice($arr,0,$words);
	return join(' ',$arr);
 }
?>
	<div id="menu_tool">
    	<div id="tool_icon">
        	<ul>
            	<li><a href="?go=addnhomnguoidung"><img src="img/add.png" /></a></li>
            </ul>
            <ul>
                <li style="padding:0 5px 0 0px"><a href="?go=addnhomnguoidung">Thêm Mới</a></li>
            </ul>
        </div>
    </div>	
    <?									

				$action = $_REQUEST['action']; 
			
					switch($action) {
						case "search":
						{							
							if (!isset($_REQUEST['tennhom']) && !isset($_REQUEST['trangthai'])) {
								$tennhom = $_SESSION['tennhom'];	
								$trangthai = $_SESSION['trangthai'];
							} else {
								$tennhom = $_REQUEST['tennhom'];
								$trangthai = $_REQUEST['trangthai'];
								$_SESSION['tennhom'] = $tennhom;
								$_SESSION['trangthai'] = $trangthai;
							}
							$sqlSearch = " WHERE tennhom LIKE '%$tennhom%'";
							if ($trangthai < 2) { 
								$sqlSearch .= " AND status = '$trangthai' ";
							}
							$_SESSION["search"] = $sqlSearch;
						}
						break;
						
						case "view":
						{
							$_SESSION["search"] = "";

						}
						break;
						
						case 'add':
						{
							$tennhom = $_REQUEST['tennhom'];
					//		$thoigian = $_REQUEST['thoigian'];
							$status = $_REQUEST['trangthai'];
							$sql="insert nhomnguoidung(tennhom, thoigian, status) values('$tennhom', now() , $status)";
							$result=mysql_query($sql) or die("Can not insert nhomnguoidung" .mysql_error($conn));
							echo "<script> location='?go=nhomnguoidung&action=view&page=1';</script>";
						}
						break;
						
						case "del":
						{
								$id = $_REQUEST['id'];
								$sql="select * from nhomnguoidung where idnhomnguoidung= $id";
								$result=mysql_query($sql) or die("Can not query delete nhomnguoidung".mysql_error($conn));
								$row=mysql_fetch_array($result);
								if(mysql_num_rows($result)!= 0)
								{
									$sql="delete from nhomnguoidung where idnhomnguoidung = $id";
									$result=mysql_query($sql) or die("Can not delete nhomnguoidung".mysql_error($conn));
								}
								else
								{
									echo "<script> alert('Không thể xóa '); </script>";
								}
								echo "<script>alert('Xóa thành công'); location='?go=nhomnguoidung&action=view&page=1';</script>";
						}
						break;
						
						case "edit":
						{
								$id = $_REQUEST['id'];
								$tennhom = $_REQUEST['tennhom'];
								
								$status = $_REQUEST['trangthai'];
								$sql="select * from nhomnguoidung where tennhom ='$tennhom' and idnhomnguoidung!=$id";
								$result=mysql_query($sql) or die("Can not query nhomnguoidung in edit");
								if(mysql_num_rows($result)!=0)
									echo "<script>alert('Tên nhóm đã tồn tại, nhập tên nhóm khác'); window.history.go(-1);</script>";
								else
								{
									$sql="update nhomnguoidung set tennhom ='$tennhom',status = $status where idnhomnguoidung=$id";
									$result=mysql_query($sql) or die("Can not update nhomnguoidung".mysql_error($conn));
									echo "<script>alert('Sửa thành công');</script>";		
								}
								echo "<script> location='?go=nhomnguoidung&action=view&page=1';</script>";
						}
						break;
					}
			
		?>
     
        <div id="bang">
    	<table border="1px"  bordercolor="#CCCCCC" cellpadding="0px" cellspacing="0px" cols="8" width="790px">
        		<tr>
				<td colspan="10" align="left"> &nbsp;<input type="button" value="Lọc Dữ Liệu" id="TimKiem" onclick="clickTimKiem()" style="padding:0 5px" /> 
				<form name="formTimKiem" id="formTimKiem" method="post" action="?go=nhomnguoidung&action=search&page=1" style="display:none">
				<br />
					<table cellpadding="0" cellspacing="0" align="center" width="100%" border="0">
						<tr>
							<td width="40%" align="right"> Tên nhóm : &nbsp;</td>
							<td width="60%" align="left"> &nbsp;<input type="text" name="tennhom"  /> </td>
						</tr>
						<tr>
							<td align="right"> Trạng thái : &nbsp;</td>
							<td> &nbsp;<select name="trangthai">
								<option value = "2" selected="selected">Tất cả</option>
								<option value="1" >Hoạt động</option>
								<option value="0">Không hoạt động</option>
					
       			 				</select></td>
						</tr>
						<tr>
							<td colspan="2" align="center"> <input type="submit" value="Lọc dữ liệu" style="padding:0 5px" /></td>
						</tr>
					</table>
				<br />
				</form>
				</td>
              
			</tr>
           <!-- <tr>
            	<td colspan="4"></td>
            	<td colspan="2"><input type="button" id="themmoi" value="Thêm Mới" style="padding: 0 5px"  /></td>
                <td colspan="2"><input type="button" id="kichhoat" value="Kích Hoạt" style="padding: 0 5px"  /></td>
                <td colspan="2"><input type="button" id="vohieu" value="Vô Hiệu" style="padding: 0 5px"  /></td>                
            </tr>-->
			
            <th style="width:30px">STT</th>
            <th style="width:380px">Tên Nhóm</th>
            <th style="width:40px">Trạng Thái<th>
            <th style="width:120px">Ngày Tạo</th>
            <th style="width:30px">Sửa</th>
 			<th style="width:30px">Xóa</th>
            <th style="width:80px">Kích Hoạt</th>
            <th style="width:80px">Vô Hiệu</th>
        <?
			
			$display  = 10;
			$pagenum = $_REQUEST["page"];
			$i  = 0;
			$sql = "SELECT * FROM nhomnguoidung ";
			$sql .= $_SESSION["search"];
			$queryCount = mysql_query($sql) or die("Can not query count nhomnguoidung ".mysql_error($conn));
			$count = mysql_num_rows($queryCount);
			$sql .= " ORDER BY idnhomnguoidung ASC";
			$result=mysql_query($sql) or die("Can not query nhomnguoidung Order".mysql_error($conn));
				$totalpage=ceil(mysql_num_rows($result)/$display);
				if($pagenum<=0 ||$pagenum > $totalpage)
					$pagenum=1;
				$from=($pagenum-1)*$display;
				$sql.="  LIMIT $from, $display";
				$query=mysql_query($sql)  or die("Can not query nhomnguoidung Limit".mysql_error($conn));;	
			while ($row=mysql_fetch_array($query)) 
			{
				$i++;
				$temp = $row['idnhomnguoidung'];
				
		?>
        	<tr>
            	<td class="stt"><? echo ($from+$i); ?></td>
                <td class="tennhom" title="<?=$row['tennhom']?>"><?php echo  $row['tennhom'];?> </td>
                <td class="trangthai" align="center"><? if ($row['status'] == 1) {
					echo '<img src="img/Check.jpg"/>';	
				} else {
					echo '<img src="img/Delete.jpg"/>';
				}
				?>
				</td>
                <td></td>
                 <td class="thoigian"><?php echo ($time = date('d-m-Y G:i:s',strtotime($row['thoigian'])));?></td>
                <td class="chucnang" align="center"> <input type="button" value="Sửa" onClick="location='?go=editnhomnguoidung&id=<?php echo $temp; ?>';"  style="width:100%"/></td>
				<td align="center"> <input type="button" value="Xoá" onClick="if (confirm('Bạn chắc chắn muốn xóa?') == true)
				{
					location='?go=nhomnguoidung&action=del&id=<?php echo $temp; ?>';			
				}
				else
				return false;"  style="width:100%"/></td>
        		<td class="chucnang" align="center"> <input type="button" value="Kích Hoạt" onClick="if (confirm('Bạn muốn kích hoạt?') == true) {
                	location='?go=sttnhomnguoidung&action=enable&id=<?php echo $temp; ?>';
                } else {
                	return false;
                }" style="width:90%" /></td>
                <td class="chucnang" align="center"> <input type="button" value="Vô Hiệu" onClick="if (confirm('Bạn muốn vô hiệu hóa?') == true) {
                location='?go=sttnhomnguoidung&action=disable&id=<?php echo $temp; ?>';
                } else return false;"  style="width:90%" /></td>        
            </tr>
        <?		
			} 
			//end while 
			
			?>
             <tr>
				<td colspan="3">Tổng số bản ghi: <? echo $count ?> </td>
                <td colspan="7">Số bản ghi/trang: <? echo $display ?></td>     	
                </td>
			</tr>
            <tr class="pagelist">
            	<td colspan="9">
                <? 
				for($i=1;$i<=$totalpage;$i++)
				{
					if ($action == 'view') {
					if($i==1)
						echo "<a href='?go=nhomnguoidung&action=view&page=$i'>".$i."</a>";
					else
						echo " | <a href='?go=nhomnguoidung&action=view&page=$i'>".$i."</a>";
				
				} else {
					if($i==1)
						echo "<a href='?go=nhomnguoidung&action=search&page=$i'>".$i."</a>";
					else
						echo " | <a href='?go=nhomnguoidung&action=search&page=$i'>".$i."</a>";
				}
				}
                ?>
                </td>
             </tr>

  			
      </table>
          <? 
	
		  ?> 
    </div>
   
	<div class="clear"></div>
</div>
          
